home *** CD-ROM | disk | FTP | other *** search
/ Mastering Computers 3 / Mastering Computers Vol 3.iso / Win95 / Fun&Utils / GLFONT.EXE / FONT.C next >
Encoding:
C/C++ Source or Header  |  1995-05-31  |  8.7 KB  |  319 lines

  1. //****************************************************************************
  2. // File: glthread.c
  3. //
  4. // Purpose: Contains code to set up the pixel format and initialize OpenGL.  
  5. //          Also contains code to rotate and draw three-dimensional TrueType
  6. //          characters from display lists created with wglUseFontOutlines().
  7. //
  8. // Development Team:
  9. //         Greg Binkerd - Windows Developer Support
  10. //
  11. // Written by Microsoft Windows Developer Support
  12. // Copyright (c) 1995 Microsoft Corporation. All rights reserved.
  13. //****************************************************************************
  14.  
  15. #include "glfont.h"
  16.  
  17. // Handle to main window
  18. extern HWND ghWnd;
  19.  
  20. // Boolean indicating if it is safe to draw or not
  21. extern BOOL bDraw;
  22.  
  23. HPALETTE ghpalOld, ghPalette = (HPALETTE) 0;
  24. GLfloat radius;
  25. RECT    oldrect;
  26.  
  27. unsigned char threeto8[8] = {
  28.     0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377
  29. };
  30.  
  31. unsigned char twoto8[4] = {
  32.     0, 0x55, 0xaa, 0xff
  33. };
  34.  
  35. unsigned char oneto8[2] = {
  36.     0, 255
  37. };
  38.  
  39. static int defaultOverride[13] = {
  40.     0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91
  41. };
  42.  
  43. static PALETTEENTRY defaultPalEntry[20] = {
  44.     { 0,   0,   0,    0 },
  45.     { 0x80,0,   0,    0 },
  46.     { 0,   0x80,0,    0 },
  47.     { 0x80,0x80,0,    0 },
  48.     { 0,   0,   0x80, 0 },
  49.     { 0x80,0,   0x80, 0 },
  50.     { 0,   0x80,0x80, 0 },
  51.     { 0xC0,0xC0,0xC0, 0 },
  52.  
  53.     { 192, 220, 192,  0 },
  54.     { 166, 202, 240,  0 },
  55.     { 255, 251, 240,  0 },
  56.     { 160, 160, 164,  0 },
  57.  
  58.     { 0x80,0x80,0x80, 0 },
  59.     { 0xFF,0,   0,    0 },
  60.     { 0,   0xFF,0,    0 },
  61.     { 0xFF,0xFF,0,    0 },
  62.     { 0,   0,   0xFF, 0 },
  63.     { 0xFF,0,   0xFF, 0 },
  64.     { 0,   0xFF,0xFF, 0 },
  65.     { 0xFF,0xFF,0xFF, 0 }
  66. };
  67.  
  68. //***********************************************************************
  69. // Function: ComponentFromIndex
  70. //
  71. //
  72. // Comments: Taken from the "GenGL" OpenGL sample application
  73. //
  74. //****************************************************************************
  75. unsigned char ComponentFromIndex(int i, UINT nbits, UINT shift)
  76. {
  77.     unsigned char val;
  78.  
  79.     val = (unsigned char) (i >> shift);
  80.     switch (nbits) {
  81.  
  82.     case 1:
  83.         val &= 0x1;
  84.         return oneto8[val];
  85.  
  86.     case 2:
  87.         val &= 0x3;
  88.         return twoto8[val];
  89.  
  90.     case 3:
  91.         val &= 0x7;
  92.         return threeto8[val];
  93.  
  94.     default:
  95.         return 0;
  96.     }
  97. }
  98.  
  99. //***********************************************************************
  100. // Function: CreateRGBPalette 
  101. //
  102. //
  103. // Comments: Taken from the "GenGL" OpenGL sample application
  104. //
  105. //****************************************************************************
  106. void CreateRGBPalette(HDC hDC)
  107. {
  108.     PIXELFORMATDESCRIPTOR pfd;
  109.     LOGPALETTE *pPal;
  110.     int n, i;
  111.  
  112.     n = GetPixelFormat(hDC);
  113.     DescribePixelFormat(hDC, n, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  114.  
  115.     if (pfd.dwFlags & PFD_NEED_PALETTE) {
  116.         n = 1 << pfd.cColorBits;
  117.         pPal = (PLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
  118.                 n * sizeof(PALETTEENTRY));
  119.         pPal->palVersion = 0x300;
  120.         pPal->palNumEntries = n;
  121.         for (i=0; i<n; i++) {
  122.             pPal->palPalEntry[i].peRed =
  123.                     ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift);
  124.             pPal->palPalEntry[i].peGreen =
  125.                     ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift);
  126.             pPal->palPalEntry[i].peBlue =
  127.                     ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift);
  128.             pPal->palPalEntry[i].peFlags = 0;
  129.         }
  130.  
  131.         /* fix up the palette to include the default GDI palette */
  132.         if ((pfd.cColorBits == 8)                           &&
  133.             (pfd.cRedBits   == 3) && (pfd.cRedShift   == 0) &&
  134.             (pfd.cGreenBits == 3) && (pfd.cGreenShift == 3) &&
  135.             (pfd.cBlueBits  == 2) && (pfd.cBlueShift  == 6)
  136.            ) {
  137.             for (i = 1 ; i <= 12 ; i++)
  138.                 pPal->palPalEntry[defaultOverride[i]] = defaultPalEntry[i];
  139.         }
  140.  
  141.         ghPalette = CreatePalette(pPal);
  142.         LocalFree(pPal);
  143.  
  144.         ghpalOld = SelectPalette(hDC, ghPalette, FALSE);
  145.         n = RealizePalette(hDC);
  146.     }
  147. }
  148.  
  149. //***********************************************************************
  150. // Function: bSetupPixelFormat
  151. //
  152. //
  153. // Comments: Taken from the "GenGL" OpenGL sample application
  154. //
  155. //****************************************************************************
  156. BOOL bSetupPixelFormat(HDC hDC)
  157. {
  158.     static PIXELFORMATDESCRIPTOR pfd = {
  159.     sizeof(PIXELFORMATDESCRIPTOR),    // size of this pfd
  160.     1,                // version number
  161.     PFD_DRAW_TO_WINDOW |        // support window
  162.     PFD_DOUBLEBUFFER |
  163.     PFD_SUPPORT_OPENGL,        // support OpenGL
  164.     PFD_TYPE_RGBA,            // RGBA type
  165.     24,                // 24-bit color depth
  166.     0, 0, 0, 0, 0, 0,        // color bits ignored
  167.     0,                // no alpha buffer
  168.     0,                // shift bit ignored
  169.     0,                // no accumulation buffer
  170.     0, 0, 0, 0,             // accum bits ignored
  171.     32,                // 32-bit z-buffer    
  172.     0,                // no stencil buffer
  173.     0,                // no auxiliary buffer
  174.     PFD_MAIN_PLANE,            // main layer
  175.     0,                // reserved
  176.     0, 0, 0                // layer masks ignored
  177.     };
  178.     int pixelformat;
  179.  
  180.     if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
  181.     {
  182.         MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
  183.         return FALSE;
  184.     }
  185.  
  186.     if (SetPixelFormat(hDC, pixelformat, &pfd) == FALSE)
  187.     {
  188.         MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
  189.         return FALSE;
  190.     }
  191.  
  192.     CreateRGBPalette(hDC);
  193.  
  194.     return TRUE;
  195. }
  196.  
  197.  
  198. //***********************************************************************
  199. // Function: initialize
  200. //
  201. // Purpose: Called by Windows on app startup.  Initializes everything,
  202. //          and enters a message loop.
  203. //
  204. // Parameters:
  205. //    hWnd     == Handle to window
  206. //
  207. // Returns: none
  208. //
  209. // Comments:
  210. //
  211. // History:  Date       Author        Reason
  212. //           3/20/95     GGB          Created
  213. //****************************************************************************
  214.  
  215. GLvoid initialize(HWND hWnd)
  216. {
  217.     GLfloat    maxObjectSize, aspect;
  218.     GLdouble    near_plane, far_plane;
  219.  
  220.     GetClientRect(hWnd, &oldrect);
  221.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  222.  
  223.     glEnable(GL_DEPTH_TEST);
  224.  
  225.     glMatrixMode( GL_PROJECTION );
  226.     aspect = (GLfloat) oldrect.right / oldrect.bottom;;
  227.     gluPerspective( 15.0, aspect, 2.0, -2.0 );
  228.     glMatrixMode( GL_MODELVIEW );
  229.  
  230.     near_plane = 2.0;
  231.     far_plane = -2.0;
  232.     maxObjectSize = 2.0;
  233.     radius = near_plane + maxObjectSize/2.0;
  234. }
  235.  
  236.  
  237.  
  238. //***********************************************************************
  239. // Function: draw_scene
  240. //
  241. // Purpose:  Called when a WM_TIMER message is sent to the main window.  It
  242. //           will draw three-dimensional TrueType characters from display
  243. //           lists created with wglUseFontOutlines().
  244. //
  245. // Parameters: hWnd == main window handle
  246. //
  247. // Returns: none
  248. //
  249. // Comments:
  250. //
  251. // History:  Date       Author        Reason
  252. //           5/31/95     GGB          Created
  253. //****************************************************************************
  254.  
  255. GLvoid draw_scene(HWND hWnd)
  256. {
  257.    HDC hdc;
  258.    // Static GLfloat's to hold the current rotation handles
  259.    static GLfloat  fAngY = 5.0f;
  260.    static GLfloat  fAngX = 1.0f;
  261.    static GLfloat  fAngZ = 3.0f;
  262.    
  263.    // We don't want re-entrancy
  264.    bDraw = FALSE;
  265.  
  266.    // Increase the angles so we can rotate the text
  267.    fAngX += 5.0f;
  268.    fAngY += 5.0f;
  269.    fAngZ += 2.0f;
  270.  
  271.    // Clear the OpenGL window's client area
  272.    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  273.    glPushMatrix();
  274.    glTranslatef(0.0, 0.0, -radius);
  275.    // rotate
  276.    glRotatef(fAngX, 1.0f, 0.0f, 0.0f);
  277.    glRotatef(fAngY, 0.0f, 1.0f, 0.0f);
  278.    glRotatef(fAngZ, 0.0f, 0.0f, 1.0f);
  279.  
  280.    glScalef(0.3f, 0.3f, 0.3f);
  281.    
  282.  
  283.    // Display a string with the display lists created by wglUseFontOutlines()
  284.    glColor3f(1.0,0.5,0.5);
  285.    glListBase(GLF_START_LIST); // indicate the start of display lists for the glyphs.
  286.    // Draw the characters
  287.    glCallLists(6, GL_UNSIGNED_BYTE, "OpenGL");
  288.   
  289.    glPopMatrix();
  290.    glFinish();
  291.    // We are double-buffering, so we need to swap the buffers
  292.    hdc = wglGetCurrentDC();
  293.    SwapBuffers(hdc);
  294.    bDraw = TRUE;
  295. }
  296.  
  297.  
  298. //***********************************************************************
  299. // Function: resize
  300. //
  301. //
  302. // Comments: Taken from the "GenGL" OpenGL sample application.  Modified to
  303. //           not generate a WM_PAINT message.
  304. //
  305. //****************************************************************************
  306. GLvoid resize(HWND hWnd)
  307. {
  308.     RECT    rect;
  309.  
  310.     GetClientRect(hWnd, &rect);
  311.  
  312.     glViewport(0, 0, rect.right, rect.bottom);
  313.  
  314.     oldrect.right = rect.right;
  315.     oldrect.bottom = rect.bottom;
  316. }
  317.  
  318.  
  319.